//
//  YYUserFeedbackView.m
//  Project
//
//  Created by Yown on 2024/2/5.
//

#import "YYUserFeedbackView.h"

#import <Masonry/Masonry.h>
#import <YYKit/YYKit.h>
#import <BINetworkModule/BINetworkManager.h>

#import <BIToolModule/BIAlertAction.h>
#import <BIToolModule/UIView+Custom.h>
#import <BIToolModule/BIProjectDefine.h>
#import <BIProjectModule/BIAppDefine.h>
#import <BIToolModule/NSArray+Helper.h>
#import <BIToolModule/NSMutableDictionary+Helper.h>
#import <BIToolModule/NSObject+ViewController.h>

@interface YYUserFeedbackView () <YYTextViewDelegate, UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UIView *feedbackContentView;
@property (weak, nonatomic) IBOutlet UILabel *feedbackLabel;
@property (weak, nonatomic) IBOutlet UIView *typeAndPhoneView;
@property (weak, nonatomic) IBOutlet UIView *typeView;
@property (weak, nonatomic) IBOutlet UILabel *typeLabel;
@property (weak, nonatomic) IBOutlet UILabel *selectedTypeLabel;
@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UIButton *commitButton;
@property (nonatomic) YYTextView *textView;

@property (nonatomic, copy) NSArray <YYFeedBackTypeModel *>*modelArray;
@property (nonatomic, assign) NSInteger selectedIndex;

@end

@implementation YYUserFeedbackView

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.backgroundColor = BIColor(BIAppMainBGColor);
    
    self.feedbackContentView.layer.masksToBounds = YES;
    self.feedbackContentView.layer.cornerRadius = 12;
    self.feedbackLabel.textColor = [UIColor loadColorFromString:@"#333333"];
    self.feedbackLabel.font = BIFont(18);
    self.feedbackLabel.text = BILocalizedString(@"user_feedback_content");
    
    self.typeAndPhoneView.layer.masksToBounds = YES;
    self.typeAndPhoneView.layer.cornerRadius = 12;
    
    self.typeView.userInteractionEnabled = YES;
    [self.typeView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickTypeView)]];
    self.typeLabel.textColor = [UIColor loadColorFromString:@"#333333"];
    self.typeLabel.font = BIFont(16);
    self.typeLabel.text = BILocalizedString(@"user_feedback_type");
    
    self.selectedTypeLabel.text = @"";
    self.selectedTypeLabel.textColor = [UIColor loadColorFromString:@"#333333"];
    self.selectedTypeLabel.font = BIFont(16);
    
    self.phoneLabel.textColor = [UIColor loadColorFromString:@"#333333"];
    self.phoneLabel.font = BIFont(16);
    self.phoneLabel.text = BILocalizedString(@"user_feedback_phone");
    
    self.phoneTextField.delegate = self;
    self.phoneTextField.returnKeyType = UIReturnKeyDone;
    self.phoneTextField.placeholder = BILocalizedString(@"user_feedback_phone_placeholder");
    self.phoneTextField.font = BIFont(16);

    self.textView = [[YYTextView alloc] initWithFrame:CGRectMake(15, 60, BIScreenWidth - 74, 240)];
    self.textView.delegate = self;
    self.textView.font = BIFont(16);
    self.textView.textColor = [UIColor loadColorFromString:@"#333333"];
    self.textView.placeholderFont = BIFont(16);
    self.textView.placeholderTextColor = [UIColor loadColorFromString:@"#BDBDBD"];
    self.textView.placeholderText = BILocalizedString(@"user_feedback_content_placeholder");
    self.textView.returnKeyType = UIReturnKeyDone;
    [self.feedbackContentView addSubview:self.textView];
    
    self.commitButton.titleLabel.font = BIFont(16);
    self.commitButton.layer.masksToBounds = YES;
    self.commitButton.layer.cornerRadius = 24;
    [self.commitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.commitButton setTitle:BILocalizedString(@"common_confirm") forState:UIControlStateNormal];
    [self switchCommitButtonState];
    
    self.selectedIndex = -1;
}

- (void)reloadType:(NSArray<YYFeedBackTypeModel *> *)typeArray {
    self.modelArray = typeArray;
}

#pragma mark - YYTextViewDelegate

- (BOOL)textView:(YYTextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    [self switchCommitButtonState];
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    } else {
        return YES;
    }
}

#pragma mark - UITextField

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    [self switchCommitButtonState];
    return YES;
}

#pragma mark - Action

- (void)clickTypeView {
    NSMutableArray *textArray = [NSMutableArray array];
    [self.modelArray enumerateObjectsUsingBlock:^(YYFeedBackTypeModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [textArray addObject:obj.label];
    }];
    [textArray addObject:BILocalizedString(@"common_cancel")];
    
    [BIAlertAction showAlertOrActionSheet:UIAlertControllerStyleActionSheet title:nil message:nil actions:textArray clickActionBlock:^(NSUInteger index) {
        if (index < textArray.count - 1) {
            self.selectedIndex = index;
            self.selectedTypeLabel.text = [textArray safeObjectAtIndex:index];
        }
    }];
}

- (IBAction)clickCommitButton:(id)sender {
    NSString *path = @"/api/app/feedback/submit";
    NSMutableDictionary *arguments = [NSMutableDictionary dictionary];
    if (self.selectedIndex != -1) {
        YYFeedBackTypeModel *model = [self.modelArray safeObjectAtIndex:self.selectedIndex];
        [arguments safeSetObject:model.type key:@"type"];
    }
    [arguments safeSetObject:self.textView.text key:@"content"];
    [arguments safeSetObject:self.phoneTextField.text key:@"contactInfo"];

    BINetworkRequest *request = [[BINetworkManager sharedInstance] requestWithPath:path arguments:arguments succeedBlock:^(BINetworkResponse *response) {
        [[NSObject currentVC].navigationController popViewControllerAnimated:YES];
    } failureBlock:nil completionBlock:nil];
    [[BINetworkManager sharedInstance] startRequest:request];
}

#pragma mark - Private Method

- (void)switchCommitButtonState {
    if (self.textView.text.length > 0 && self.phoneTextField.text > 0) {
        self.commitButton.userInteractionEnabled = YES;
        self.commitButton.backgroundColor = BIColor(BIAppMainColor);
    } else {
        self.commitButton.userInteractionEnabled = NO;
        self.commitButton.backgroundColor = [UIColor loadColorFromString:@"#E0E0E0"];
    }
}

@end
